package cn.weiyin.group07.controller;

import cn.weiyin.group07.result.Result;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.UUID;

//文件上传用模块
@RestController
@RequestMapping("/files")
@CrossOrigin(origins = "*")
public class FileController {
//    文件上传
    @PostMapping("/")
    public Result<String> fileUpLoad(@RequestParam("file") MultipartFile file) {
//        生成UUID处理图片重名问题
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        String originalFilename = file.getOriginalFilename();
        String saveFileName = "/picture/" + uuid + "_"+ originalFilename;
        try {
            FileOutputStream stream = new FileOutputStream("src/main/resources/static" + saveFileName);
            stream.write(file.getBytes());
            stream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return Result.success(saveFileName,"上传成功");
    }

}
